package com.flag.liutao.modules.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.enums.MenuTypeEnum;
import com.flag.liutao.common.enums.SysEnum;
import com.flag.liutao.modules.role.domain.Menu;
import com.flag.liutao.modules.role.domain.RoleMenu;
import com.flag.liutao.modules.role.mapper.MenuMapper;
import com.flag.liutao.modules.role.mapper.RoleMenuMapper;
import com.flag.liutao.modules.role.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.role.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-02
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 通过角色id 获取权限列表
     * @param id
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    @Override
    public Map<String, Object> getRoleMenuPage(Long id, int page, int limit, String keyword) {
        LambdaQueryWrapper<Menu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Menu::getStatus, SysEnum.STATUS_1.getValue());
        lqw.and(lqw1->{
            lqw1.like(Menu::getMenuName,keyword).or().like(Menu::getMenuKey,keyword);
        });
        IPage<Menu> iPage = new Page<>(page,limit);
        IPage<Menu> pageDB = menuMapper.getRoleMenuPageById(id, iPage,keyword);
        Map<String,Object> map = new LinkedHashMap<>();
        List<MenuVo> list = pageDB.getRecords().stream().map(menu -> {
            MenuVo menuVo = new MenuVo();
            BeanUtil.copyProperties(menu,menuVo);
            return menuVo;
        }).collect(Collectors.toList());
        map.put("content",list);
        map.put("total",pageDB.getTotal());
        return map;
    }

    /**
     * 删除角色权限关系
     * @param roleId
     * @param menuId
     */
    @Override
    public void delRoleMenu(Long roleId, Long menuId) {
        LambdaQueryWrapper<RoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(RoleMenu::getMenuId,menuId).eq(RoleMenu::getRoleId,roleId);
        roleMenuMapper.delete(lqw);
    }

    /**
     * 通过角色id 获取未授权权限列表
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getRoleNoHasList(Long id) {
        List<Menu> roleMenuList = getRoleMenuList(id);
        List<Menu> menuListDB = menuMapper.selectList(new LambdaQueryWrapper<Menu>().eq(Menu::getStatus,SysEnum.STATUS_1.getValue()));
        List<Menu> list = new ArrayList<>();
        for (Menu menu : menuListDB) {
            Boolean flag = false;
            for (Menu roleMenu : roleMenuList) {
                if(roleMenu.getId() == menu.getId()){
                    flag = true;
                    break;
                };
            }
            if(!flag) list.add(menu);
        }
        List<MenuVo> powerList = new ArrayList<>();List<MenuVo> rootList =new ArrayList<>();List<MenuVo> menuList =new ArrayList<>();Iterator<Menu> iterator = list.iterator();
        while (iterator.hasNext()) {
            Menu menu = iterator.next();
            MenuVo menuVo = new MenuVo();
            BeanUtil.copyProperties(menu,menuVo);
            if(menu.getType().equals(MenuTypeEnum.POWER.getValue())){
                powerList.add(menuVo);
            }else if(menu.getPid() ==0){
                rootList.add(menuVo);
            }else {
                menuList.add(menuVo);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("menuList",menuList);map.put("rootList",rootList);map.put("powerList",powerList);
        return map;
    }

    /**
     * 添加角色权限关系
     * @param roleId
     * @param menuIdList
     */
    @Override
    public void addRoleMenu(Long roleId, List<Long> menuIdList) {
        List<RoleMenu> roleMenuList = new ArrayList<>();
        for (Long id : menuIdList) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(id);
            roleMenuList.add(roleMenu);
        }
        this.saveBatch(roleMenuList);
    }

    /**
     * 获取所有授权权限
     * @param roleId
     * @return
     */
    public List<Menu> getRoleMenuList(Long roleId){
        List<Menu> roleMenuList = menuMapper.getRoleMenuListById(roleId);
        return roleMenuList;
    }


}
